package com.jier.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2配置文件
 *
 * @author eayon
 * @date 2017-10-31
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SpringfoxConfig {
    /**
     * 商城api组
     */
    public static String GROUP_MALL = "default";

    /**
     * 商城api
     *
     * @return
     */
    @Bean
    public Docket mallApi() {
        List<Parameter> pars = globalParam();
        List<ResponseMessage> responseMessages = getResponseMessages();
        return generateDocket(GROUP_MALL, "com.jier.controller", pars, responseMessages, mallApiInfo());
    }

    private ApiInfo mallApiInfo() {
        return new ApiInfoBuilder().title("商城 API").description("").termsOfServiceUrl("http://localhost:9999").version("1.0").build();
    }

    /**
     * 生成Docket
     *
     * @param groupName
     * @param basePackage
     * @param pars
     * @param responseMessages
     * @param apiInfo
     * @return
     */
    public Docket generateDocket(String groupName, String basePackage, List<Parameter> pars, List<ResponseMessage> responseMessages, ApiInfo apiInfo) {
        // paths: 生产环境改为none
        return new Docket(DocumentationType.SWAGGER_2).groupName(groupName).apiInfo(apiInfo).select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .build()
                .globalOperationParameters(pars)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.DELETE, responseMessages)
                .globalResponseMessage(RequestMethod.PATCH, responseMessages)
                .apiInfo(apiInfo);
    }

    /**
     * 设置全局返回消息体
     *
     * @return
     */
    public List<ResponseMessage> getResponseMessages() {
        List<ResponseMessage> responseMessages = new ArrayList<ResponseMessage>();
        // HttpStatus.NOT_FOUND ==> 404
        ResponseMessageBuilder responseMessageBuilderNotFound = new ResponseMessageBuilder();
        responseMessageBuilderNotFound.code(HttpStatus.NOT_FOUND.value()).message("访问路径不存在");
        responseMessages.add(responseMessageBuilderNotFound.build());
        // HttpStatus.METHOD_NOT_ALLOWED ==> 405
        ResponseMessageBuilder responseMessageBuilderMethodNotAllowed = new ResponseMessageBuilder();
        responseMessageBuilderMethodNotAllowed.code(HttpStatus.METHOD_NOT_ALLOWED.value()).message("接口不支持当前请求方式");
        responseMessages.add(responseMessageBuilderMethodNotAllowed.build());
        // HttpStatus.INTERNAL_SERVER_ERROR ==> 500
        ResponseMessageBuilder responseMessageBuilderERROR = new ResponseMessageBuilder();
        responseMessageBuilderERROR.code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("服务器异常");
        responseMessages.add(responseMessageBuilderERROR.build());
        return responseMessages;
    }

    /**
     * 设置全局header添加token，默认为必填（必填>非必填），
     * 非必填接口手动添加@ApiImplicitParam(name = "token", value = "令牌", dataType = "String", paramType = "header", required = false)
     *
     * @return
     */
    public List<Parameter> globalParam() {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(true);
        pars.add(tokenPar.build());
        return pars;
    }
}